package com.yonyougov.devops.cruxcisupport.roleapp;

import com.yonyougov.devops.cruxcisupport.roleapp.dto.AddMulRoleAppDto;
import com.yonyougov.devops.cruxcisupport.roleapp.dto.AddMulRoleAppRequestDto;
import com.yonyougov.devops.cruxcisupport.roleapp.dto.RoleAppPageLikeRequestDto;
import com.yonyougov.devops.cruxcisupport.roleapp.dto.deleteMulRoleAppByPrimaryKeyRequestDto;
import com.yonyougov.devops.cruxcisupport.roleapp.vo.RoleAppPageByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.roleapp.vo.RoleAppVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.CrudController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import com.yonyougov.devops.fwc.core.service.BaseService;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigInteger;

/**
 * Created by ligson on 2017/8/11.
 */
@RestController
@RequestMapping("/api/v2/roleapp")
public class RoleAppController extends CrudController<RoleAppEntity, BigInteger> {

    private final RoleAppService roleAppService;


    public RoleAppController(RoleAppService roleAppService) {
        this.roleAppService = roleAppService;
    }

    @Override
    public BaseService<RoleAppEntity, BigInteger> getService() {
        return roleAppService;
    }

    //<!--用户自定义代码开始-->

    @ApiOperation(value = "角色模块关联应用调用", notes = "可以新增多个角色应用映射关系")
    @PostMapping(value = "/ra/add/batch")
    public BaseResponseDto addBatchRoleApp(@RequestBody @Valid AddMulRoleAppRequestDto request) {
        AddMulRoleAppDto addMulRoleAppDto = new AddMulRoleAppDto();
        BeanUtils.copyProperties(request, addMulRoleAppDto);

        roleAppService.addMulRoleApp(addMulRoleAppDto);

        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "角色模块关联应用调用", notes = "可选择根据应用名称进行模糊分页查询")
    @PostMapping(value = "/ra/page/like")
    @SneakyThrows
    public BaseResponseDto findRoleAppPageLike(@RequestBody @Valid RoleAppPageLikeRequestDto request) {
        RoleAppPageByRoleIdDto roleAppPageByRoleIdDto = new RoleAppPageByRoleIdDto();
        BeanUtils.copyProperties(request, roleAppPageByRoleIdDto);

        Pagination<RoleAppVo> pagination = roleAppService.findRoleAppPageByRoleId(roleAppPageByRoleIdDto);

        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "角色模块关联应用调用", notes = "可以删除多个角色应用映射关系")
    @PostMapping(value = "/ra/delete/batch/ids")
    @SneakyThrows
    public BaseResponseDto deleteMulRoleAppByPrimaryKey(@RequestBody @Valid deleteMulRoleAppByPrimaryKeyRequestDto request) {

        roleAppService.deleteRoleAppByPrimaryKey(request.getIds());

        return BaseResponseDto.getSuccessInstance();
    }

    //@ApiOperation(value = "根据角色Ids获取应用Ids,内部调用", notes = "根据角色id,获取应用Ids")
    //@PostMapping(value = "/ra/appids/roleids")
    //@SneakyThrows
    //public BaseResponseDto getAppIdsByRoleIds(@RequestBody @Valid GetAppIdsByRoleIdsReqDto request) {
    //    GetAppIdsByRoleIdsReqVo params = new GetAppIdsByRoleIdsReqVo();
    //    BeanUtils.copyProperties(request, params);
    //    List<BigInteger> appIds = roleAppService.getAppIdsByRoleIds(params);
    //    BaseResponseDto result = BaseResponseDto.getSuccessInstance();
    //    result.put("data", appIds);
    //    return result;
    //}
    //<!--用户自定义代码结束-->
}
